package com.byq.mm.smartlib.obd;

import java.util.HashMap;
import java.util.Map;


import com.byq.mm.smartlib.obd.commands.decoder.CommandDecoder;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;



public class ObdSensorAdapter extends BaseAdapter{
	ObdSensorDevice[] devices;
	Map<String, ObdSensorDevice> views = new HashMap<String, ObdSensorDevice>();
	Map<String, CommandDecoder> decoders;
	public ObdSensorAdapter(Context ctx, Map<String, CommandDecoder> ds) {
		decoders = ds;
		devices = new ObdSensorDevice[decoders.size()];
		
		int index = 0;
		
		for(String i : decoders.keySet()){
			CommandDecoder cd = decoders.get(i);
			ObdSensorDevice d = new ObdSensorDevice(ctx, cd);
			views.put(i, d);
			devices[index++] = d;
		}
	}
	public void update(CommandDecoder decoder){
		ObdSensorDevice view = views.get(decoder.getKey());
		if(view != null)
			view.onLevel(decoder.getValue());
	}
	public void update(){
		for(CommandDecoder cd : decoders.values()){
			update(cd);
		}
	}
	@Override
	public int getCount() {
		return views.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView == null)
		{
			final ObdSensorDevice se = devices[position];
			convertView = devices[position].view;

			AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
					150,200);
			convertView.setLayoutParams(lp);
			
			
		}
		return convertView;
	}
	
}